package com.fy.service.server.handler;

import com.alibaba.fastjson.JSONObject;
import com.fy.common.model.im.MessageModel;
import com.fy.service.server.service.ImService;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("websocketHandler")
@ChannelHandler.Sharable
public class WebsocketHandler extends SocketServerBusiHandler {
    static final Logger logger = LoggerFactory.getLogger(WebsocketHandler.class);
    @Autowired
    private ImService imService;

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object obj) {
        try {
            if (obj instanceof TextWebSocketFrame) {
                TextWebSocketFrame text = (TextWebSocketFrame) obj;
                System.out.println("收到消息" + text.text());
                imService.route(JSONObject.parseObject(text.text(), MessageModel.class), channelHandlerContext);
                //channelHandlerContext.write(new TextWebSocketFrame("收到收到"));
            }

        } catch (Exception e) {
            logger.error("channelRead0 :", e);
        }
    }
}


